<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\BaseController;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;

class SendController extends BaseController
{
    /**
     * 发送验证码
     * @return \Illuminate\Http\JsonResponse
     */
    public function sendMobileCode(Request $request)
    {
        if ($request->has('phone')) {
            $areacode = $request->input('areacode');
            $phone=$request->input('phone');
            $sendcode = new ChuanglanSmsApi();
            $code = mt_rand(10000, 99999);
            if (Redis::get($phone)) return $this->apiError('您的操作太頻繁，請稍後再試');
            Redis::setex($phone, 60, $code);

//            return $this->success($code);
             $result =  $sendcode->sendInternational($areacode.$phone, '【睿創星河】您的驗證碼為：' . $code);
             if (!is_null(json_decode($result))) {
                 $output = json_decode($result, true);
                 if (isset($output['code']) && $output['code'] == '0') {
                     return $this->success(["ver_code" => $code]);
                 } else {
                     return $this->apiError();
                 }
             } else {
                 return $this->apiError();
             }
        } else {
            return $this->apiError('參數不能為空');
        }
    }
}
